package com.sugon.task;


import cn.hutool.core.collection.CollectionUtil;
import com.sugon.annotation.ShiroSecurityManagerChecker;
import com.sugon.dao.DeviceInfoDao;
import com.sugon.dao.SpecialDevCheckRecordDao;
import com.sugon.entity.DeviceInfoEntity;
import com.sugon.entity.SpecialDevCheckRecordEntity;
import com.sugon.modules.sys.service.INoticeManager;
import com.sugon.service.SysConfigService;
import com.sugon.service.SysMsgService;
import com.sugon.utils.ProcessInfoEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 设备相关业务处理任务
 *
 * @author YuXD
 * @since 2020-12-17 15:57
 */
@Slf4j
@Component("deviceProcessTask")
public class DeviceProcessTask {

    @Autowired
    private SysConfigService configService;
    @Autowired
    private SpecialDevCheckRecordDao specialDevCheckRecordDao;
    @Autowired
    private SysMsgService msgService;
    @Resource
    private DeviceInfoDao deviceDao;
    @Resource
    private INoticeManager noticeManager;

    /**
     * 每天凌晨0:05分，获取本年度已达到最低提醒期限的特种设备校验记录
     * 1、获取
     * 给具有处理权限的人发送处理消息
     */
    @ShiroSecurityManagerChecker
    public void doSpecDevCheckAheadNotice() {
        log.info("开始执行特种设备校验提醒任务");
        // 获取系统配置的提前提醒天数
        int aheadNoticeDays = configService.getSpecDevCheckAheadNoticeDays();
        List<SpecialDevCheckRecordEntity> specialDevCheckRecords = specialDevCheckRecordDao.queryUnCheckedRecord(aheadNoticeDays);
        log.info("本次有{}台特种设备需要进行校验");
        if (CollectionUtil.isEmpty(specialDevCheckRecords)) {
            return;
        }
        // 遍历通知，进行提醒
        for (SpecialDevCheckRecordEntity specialDevCheckRecord : specialDevCheckRecords) {
            msgService.addRemindItem(ProcessInfoEnum.SPECDEV_CHECK_REMIND, specialDevCheckRecord.getId(), null);
        }
        log.info("特种设备校验提醒任务执行结束");
    }

    /**
     * 每天上午9点，获取需要进行养护的空压设备，提示负责人进行保养
     */
    @ShiroSecurityManagerChecker
    public void doKYDevMaintenanceNotice() {
        log.info("开始执行空压设备保养提醒任务");
        // 获取需要保养的空压设备列表
        List<DeviceInfoEntity> deviceList = deviceDao.listKYMaintenance();
        if (CollectionUtil.isEmpty(deviceList)) {
            return;
        }
        noticeManager.pushKYDevMaintenanceNotice(deviceList);
        log.info("空压设备保养提醒任务执行结束");
    }

}
